﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using nplJobsDAL.Select;
using nplJobsCommon.Common;
using System.Web.Security;
using nplJobsBLL.ProcessJob;
using System.Web.Services;

public partial class Employeer_PostedJobs : BasePage // System.Web.UI.Page
{
    PagedDataSource pagedDataSource;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (HttpContext.Current.Session["user"] != null && HttpContext.Current.Session["user"].Equals(General.Users.JobSeeker.ToString()))
        {
            Response.Redirect("../JobSeeker/EditProfile.aspx");
            return;
        }
        
        base.ReloadUserInfoSession();

        if (!IsPostBack)
        {
            Session["jobsImagePath"] = null;
            this.LoadJobs();

            this.ViewState["pageIndex"] = 0;
            this.LoadLastestJobAdd();

            this.ViewState["pageIndexDraftedJobs"] = 0;
            this.LoadDraftedJobs();

            this.ViewState["pageIndexArchivedJobs"] = 0;
            this.LoadArchivedJobs();

            Page.Title = "Posted jobs";

        }
    }

    protected void btnArchivedJob_OnClick(object sender, EventArgs e)
    {

    }


    private void LoadJobs()
    {
        if (ViewState["EmployeerAllJobs"] == null)
        {
            MembershipUser currentUser = nplJobsWebSite.Common.CurrentMembershipUser;
            if (currentUser != null)
            {
                ViewState["EmployeerAllJobs"] = SelectJobList.GetJobDetails((Guid)currentUser.ProviderUserKey);
            }
        }
    }

    #region LastestJobs
    protected void lnkBtnPrev_OnClick(object sender, EventArgs e)
    {
        if (0 == (int)this.ViewState["pageIndex"])
        {
            this.ViewState["pageIndex"] = 0;
            this.ViewState["PageCount"] = 0;
        }
        else
        {
            this.ViewState["pageIndex"] = (int)this.ViewState["pageIndex"] - 1;
        }
        this.LoadLastestJobAdd();
    }

    protected void lnkBtnNext_OnClick(object sender, EventArgs e)
    {
        if ((int)this.ViewState["PageCount"] != (int)this.ViewState["pageIndex"] + 1)
        {
            this.ViewState["pageIndex"] = (int)this.ViewState["pageIndex"] + 1;
        }
        this.LoadLastestJobAdd();
    }

    protected void gvLastestJobAdd_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "PublsishLaterImageButtonClick" || e.CommandName == "ActionPublsishLaterImageButtonClick"
             || e.CommandName == "ArchiveImageButtonClick" || e.CommandName == "ActionArchiveImageButtonClick"
            || e.CommandName == "LatestPendingImageButtonClick" || e.CommandName == "ActionPostFreePublishImageButtonClick")
        {
            HiddenField txtJobID = (HiddenField)e.Item.FindControl("txtJobID");
            if (txtJobID != null && !string.IsNullOrEmpty(txtJobID.Value))
            {
                Job job = SelectJobList.GetJobDetails(Convert.ToInt32(txtJobID.Value));
                if (job != null)
                {
                    job.JobImagePath = "../Images/no_photo_available.jpg";
                    if (e.CommandName == "PublsishLaterImageButtonClick" || e.CommandName == "ActionPublsishLaterImageButtonClick")
                        job.JobPublishStatus = 1;
                    else if (e.CommandName == "ArchiveImageButtonClick" || e.CommandName == "ActionArchiveImageButtonClick")
                        job.JobPublishStatus = -1;
                    else if (e.CommandName == "LatestPendingImageButtonClick" || e.CommandName == "ActionPostFreePublishImageButtonClick")
                        job.JobPublishStatus = 99; //Publish Successfully

                    ProcessJobEntry processJobEntry = new ProcessJobEntry();
                    processJobEntry.Job = job;
                    processJobEntry.Update();

                    ViewState["EmployeerAllJobs"] = null;
                    this.LoadLastestJobAdd();
                    this.LoadDraftedJobs();
                    this.LoadArchivedJobs();
                }
            }
        }
    }
        
    private void LoadLastestJobAdd()
    {
        if (ViewState["EmployeerAllJobs"] == null)
        {
            LoadJobs();
        }

        if (ViewState["EmployeerAllJobs"] != null)
        {
            List<Job> jobList = ViewState["EmployeerAllJobs"] as List<Job>;
            List<Job> lstLastestJob = jobList.Where(job => job.JobPublishStatus == 0 || job.JobPublishStatus == 99).ToList();

            pagedDataSource = new PagedDataSource();
            pagedDataSource.DataSource = lstLastestJob;
            pagedDataSource.PageSize = 10;
            pagedDataSource.AllowPaging = true;
            pagedDataSource.CurrentPageIndex = (int)this.ViewState["pageIndex"];

            this.ViewState["PageCount"] = pagedDataSource.PageCount;

            if (pagedDataSource.PageCount < 2)
                divLatestPaging.Style.Add("display", "none");

            gvLastestJobAdd.DataSource = pagedDataSource;
            gvLastestJobAdd.DataBind();

            lnkBtnNext.Enabled = true;
            lnkBtnNext.Attributes.Remove("class");
            lnkBtnPrev.Enabled = true;
            lnkBtnPrev.Attributes.Remove("class");
            if (0 == (int)this.ViewState["pageIndex"])
            {
                lnkBtnPrev.Enabled = false;
                lnkBtnPrev.Attributes.Add("class", "disabled");

                lnkBtnNext.Enabled = true;
                lnkBtnNext.Attributes.Remove("class");
            }
            if ((int)this.ViewState["PageCount"] == (int)this.ViewState["pageIndex"] + 1)
            {
                lnkBtnNext.Enabled = false;
                lnkBtnNext.Attributes.Add("class", "disabled");

                lnkBtnPrev.Enabled = true;
                lnkBtnPrev.Attributes.Remove("class");
            }
        }
    }

    #endregion

    #region DraftedJobs
    private void LoadDraftedJobs()
    {
        if (ViewState["EmployeerAllJobs"] == null)
        {
            LoadJobs();
        }

        if (ViewState["EmployeerAllJobs"] != null)
        {
            List<Job> jobList = ViewState["EmployeerAllJobs"] as List<Job>;
            List<Job> lstDraftedJob = jobList.Where(job => job.JobPublishStatus == 1).ToList();

            pagedDataSource = new PagedDataSource();
            pagedDataSource.DataSource = lstDraftedJob;
            pagedDataSource.PageSize = 10;
            pagedDataSource.AllowPaging = true;
            pagedDataSource.CurrentPageIndex = (int)this.ViewState["pageIndexDraftedJobs"];

            this.ViewState["PageCountDraftedJobs"] = pagedDataSource.PageCount;

            if (pagedDataSource.PageCount < 2)
                divDraftedPaging.Style.Add("display", "none");

            gvDraftedJob.DataSource = pagedDataSource;
            gvDraftedJob.DataBind();

            lnkBtnDraftedJobNext.Enabled = true;
            lnkBtnDraftedJobNext.Attributes.Remove("class");
            lnkBtnDraftedJobPrev.Enabled = true;
            lnkBtnDraftedJobPrev.Attributes.Remove("class");
            if (0 == (int)this.ViewState["pageIndexDraftedJobs"])
            {
                lnkBtnDraftedJobPrev.Enabled = false;
                lnkBtnDraftedJobPrev.Attributes.Add("class", "disabled");

                lnkBtnDraftedJobNext.Enabled = true;
                lnkBtnDraftedJobNext.Attributes.Remove("class");
            }
            if ((int)this.ViewState["PageCountDraftedJobs"] == (int)this.ViewState["pageIndexDraftedJobs"] + 1)
            {
                lnkBtnDraftedJobNext.Enabled = false;
                lnkBtnDraftedJobNext.Attributes.Add("class", "disabled");

                lnkBtnDraftedJobPrev.Enabled = true;
                lnkBtnDraftedJobPrev.Attributes.Remove("class");
            }
        }
    }
    
    protected void lnkBtnDraftedJobPrev_OnClick(object sender, EventArgs e)
    {
        if (0 == (int)this.ViewState["pageIndexDraftedJobs"])
        {
            this.ViewState["pageIndexDraftedJobs"] = 0;
            this.ViewState["PageCountDraftedJobs"] = 0;
        }
        else
        {
            this.ViewState["pageIndexDraftedJobs"] = (int)this.ViewState["pageIndexDraftedJobs"] - 1;
        }
        this.LoadDraftedJobs();
    }

    protected void lnkBtnDraftedJobNext_OnClick(object sender, EventArgs e)
    {
        if ((int)this.ViewState["PageCountDraftedJobs"] != (int)this.ViewState["pageIndexDraftedJobs"] + 1)
        {
            this.ViewState["pageIndexDraftedJobs"] = (int)this.ViewState["pageIndexDraftedJobs"] + 1;
        }
        
        this.LoadDraftedJobs();
    }

    protected void gvDraftedJob_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "ReadyProcessImageButtonClick" || e.CommandName == "ActionReadyProcessImageButtonClick"
             || e.CommandName == "DeleteJobImageButtonClick" || e.CommandName == "ActionDeleteJobImageButtonClick"
            || e.CommandName == "ArchiveDraftedImageButtonClick" || e.CommandName == "ActionArchiveDraftedImageButtonClick")
        {
            TextBox txtJobID = (TextBox)e.Item.FindControl("txtJobID");
            if (txtJobID != null && !string.IsNullOrEmpty(txtJobID.Text))
            {
                Job job = SelectJobList.GetJobDetails(Convert.ToInt32(txtJobID.Text));
                if (job != null)
                {
                    if (e.CommandName == "ReadyProcessImageButtonClick" || e.CommandName == "ActionReadyProcessImageButtonClick")
                        job.JobPublishStatus = 0;
                    else if (e.CommandName == "DeleteJobImageButtonClick" || e.CommandName == "ActionDeleteJobImageButtonClick")
                        job.IsDeleted = true;
                    else if (e.CommandName == "ArchiveDraftedImageButtonClick" || e.CommandName == "ActionArchiveDraftedImageButtonClick")
                        job.JobPublishStatus = -1;

                    ProcessJobEntry processJobEntry = new ProcessJobEntry();
                    processJobEntry.Job = job;
                    processJobEntry.Update();

                    ViewState["EmployeerAllJobs"] = null;
                    this.LoadDraftedJobs();
                    this.LoadLastestJobAdd();
                    this.LoadArchivedJobs();
                }
            }
        }
    }
    #endregion

    #region ArchiveJobs
    private void LoadArchivedJobs()
    {
        if (ViewState["EmployeerAllJobs"] == null)
        {
            LoadJobs();
        }

        if (ViewState["EmployeerAllJobs"] != null)
        {
            List<Job> jobList = ViewState["EmployeerAllJobs"] as List<Job>;
            List<Job> lstArchivedJob = jobList.Where(job => job.JobPublishStatus == -1).ToList();

            pagedDataSource = new PagedDataSource();
            pagedDataSource.DataSource = lstArchivedJob;
            pagedDataSource.PageSize = 10;
            pagedDataSource.AllowPaging = true;
            pagedDataSource.CurrentPageIndex = (int)this.ViewState["pageIndexArchivedJobs"];

            this.ViewState["PageCountArchivedJobs"] = pagedDataSource.PageCount;

            if (pagedDataSource.PageCount < 2)
                divArchivePaging.Style.Add("display", "none");

            gvArchiveJob.DataSource = pagedDataSource;
            gvArchiveJob.DataBind();

            lnkBtnArchivedJobNext.Enabled = true;
            lnkBtnArchivedJobNext.Attributes.Remove("class");
            lnkBtnArchivedJobPrev.Enabled = true;
            lnkBtnArchivedJobPrev.Attributes.Remove("class");
            if (0 == (int)this.ViewState["pageIndexArchivedJobs"])
            {
                lnkBtnArchivedJobPrev.Enabled = false;
                lnkBtnArchivedJobPrev.Attributes.Add("class", "disabled");

                lnkBtnArchivedJobNext.Enabled = true;
                lnkBtnArchivedJobNext.Attributes.Remove("class");
            }
            if ((int)this.ViewState["PageCountArchivedJobs"] == (int)this.ViewState["pageIndexArchivedJobs"] + 1)
            {
                lnkBtnArchivedJobNext.Enabled = false;
                lnkBtnArchivedJobNext.Attributes.Add("class", "disabled");

                lnkBtnArchivedJobPrev.Enabled = true;
                lnkBtnArchivedJobPrev.Attributes.Remove("class");
            }
        }
    }

    protected void lnkBtnArchivedJobPrev_OnClick(object sender, EventArgs e)
    {
        if (0 == (int)this.ViewState["pageIndexArchivedJobs"])
        {
            this.ViewState["pageIndexArchivedJobs"] = 0;
            this.ViewState["PageCountArchivedJobs"] = 0;
        }
        else
        {
            this.ViewState["pageIndexArchivedJobs"] = (int)this.ViewState["pageIndexArchivedJobs"] - 1;
        }
        this.LoadArchivedJobs();
    }

    protected void lnkBtnArchivedJobNext_OnClick(object sender, EventArgs e)
    {
        if ((int)this.ViewState["PageCountArchivedJobs"] != (int)this.ViewState["pageIndexArchivedJobs"] + 1)
        {
            this.ViewState["pageIndexArchivedJobs"] = (int)this.ViewState["pageIndexArchivedJobs"] + 1;
        }
        
        this.LoadArchivedJobs();
    }

    protected void ArchiveJob_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "ActionDeleteArchivedJobImageButtonClick" || e.CommandName == "DeleteArchiveJobImageButtonClick")
        {
            HiddenField txtJobID = (HiddenField)e.Item.FindControl("txtJobID");
            if (txtJobID != null && !string.IsNullOrEmpty(txtJobID.Value))
            {
                Job job = SelectJobList.GetJobDetails(Convert.ToInt32(txtJobID.Value));
                if (job != null)
                {
                    job.IsDeleted = true;

                    ProcessJobEntry processJobEntry = new ProcessJobEntry();
                    processJobEntry.Job = job;
                    processJobEntry.Update();

                    ViewState["EmployeerAllJobs"] = null;
                    this.LoadDraftedJobs();
                    this.LoadLastestJobAdd();
                    this.LoadArchivedJobs();
                }
            }
        }
    }
    #endregion

    [WebMethod]
    public static int GetLatestJobsCount()
    {
        int result = 0;
        MembershipUser currentUser = nplJobsWebSite.Common.CurrentMembershipUser;
        if (currentUser != null)
        {
            result = SelectJobList.GetJobDetails((Guid)currentUser.ProviderUserKey).Where(job => job.JobPublishStatus == 0 || job.JobPublishStatus == 99).Count();
        }
        return result;
    }

    [WebMethod]
    public static int GetDraftedJobsCount()
    {
        int result = 0;
        MembershipUser currentUser = nplJobsWebSite.Common.CurrentMembershipUser;
        if (currentUser != null)
        {
            result = SelectJobList.GetJobDetails((Guid)currentUser.ProviderUserKey).Where(job => job.JobPublishStatus == 1).Count();
        }
        return result;
    }

    [WebMethod]
    public static int GetArchivedJobsCount()
    {
        int result = 0;
        MembershipUser currentUser = nplJobsWebSite.Common.CurrentMembershipUser;
        if (currentUser != null)
        {
            result = SelectJobList.GetJobDetails((Guid)currentUser.ProviderUserKey).Where(job => job.JobPublishStatus == -1).Count();
        }
        return result;
    }

    protected static string GetStatus(object dataItem)
    {
        if (Convert.ToInt32(DataBinder.Eval(dataItem, "JobPublishStatus")) == 99)
            return "<span style='color: green;'>Publish Successfully</span>";
        else if (Convert.ToDateTime(Convert.ToDateTime(DataBinder.Eval(dataItem, "ApplicationDateline")).ToShortDateString()) >= Convert.ToDateTime(DateTime.Now.ToShortDateString()))
            return "<span>Publish this Job</span>";
        
        return "<span style='color: red;'>Expired</span>";
    }

    protected static string GetDraftedStatus(object dataItem)
    {
        if (Convert.ToDateTime(Convert.ToDateTime(DataBinder.Eval(dataItem, "ApplicationDateline")).ToShortDateString()) >= Convert.ToDateTime(DateTime.Now.ToShortDateString()))
            return "";

        return "<span style='color: red;'>Expired</span>";
    }

    protected static bool IsDeadlineOver(object dataItem)
    {
        if (Convert.ToInt32(DataBinder.Eval(dataItem, "JobPublishStatus")) == 99)
            return true;

        return false;
    }

    protected static bool IsArchiveDeadlineOver(object dataItem)
    {
        if (Convert.ToInt32(DataBinder.Eval(dataItem, "JobPublishStatus")) == 99 || Convert.ToDateTime(Convert.ToDateTime(DataBinder.Eval(dataItem, "ApplicationDateline")).ToShortDateString()) < Convert.ToDateTime(DateTime.Now.ToShortDateString()))
            return true;

        return false;
    }

    protected static bool MakeArchiveActive(object dataItem)
    {
        if (Convert.ToDateTime(Convert.ToDateTime(DataBinder.Eval(dataItem, "ApplicationDateline")).ToShortDateString()) >= Convert.ToDateTime(DateTime.Now.ToShortDateString()))
            return true;

        return false;
    }

    protected static bool MakePublishLaterActive(object dataItem)
    {
        if ((Convert.ToInt32(DataBinder.Eval(dataItem, "JobPublishStatus")) == 99))
            return false;
        else if (Convert.ToDateTime(Convert.ToDateTime(DataBinder.Eval(dataItem, "ApplicationDateline")).ToShortDateString()) < Convert.ToDateTime(DateTime.Now.ToShortDateString()))
            return false;

        return true;
    }

    protected static bool IsJobPublished(object dataItem)
    {
        if (Convert.ToInt32(DataBinder.Eval(dataItem, "JobPublishStatus")) == 99)
            return true;

        return false;
    }

    protected static bool GetJobStatus(object dataItem)
    {
        if ((Convert.ToInt32(DataBinder.Eval(dataItem, "JobPublishStatus")) == 99) || (Convert.ToDateTime(Convert.ToDateTime(DataBinder.Eval(dataItem, "ApplicationDateline")).ToShortDateString()) < Convert.ToDateTime(DateTime.Now.ToShortDateString())))
            return false;

        return true;
    }
}